package middol.test
/**
* @Description:    对考试答案 打乱排序
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/10/23 19:45
* @Version:        1.0
*/
class RandomOption {
    /**
     * 打乱Map元素，记录对应答案key
     * @param option
     * 如：
     * key: value
     * A:   java EE
     * B:   java SE
     * C:   java ME
     * D:   java HE
     * @param key
     */
    static void random(Map<String, String> option, String key){
        //获取key值
        Set<String> keySet = option.keySet()
        println keySet
        //将set转换成List
        ArrayList<String> keyList = new ArrayList<String>(keySet)
        //打乱List
        Collections.shuffle(keyList)
        println keyList
        HashMap<String, String> newOption = new HashMap<String, String>()
        int idx = 0
        String keyNew = ""
        for (String next: keySet){
            String randomKey = keyList.get(idx++)
            if(key == next){
                keyNew = randomKey
            }
            newOption.put(randomKey, option.get(next))
            println newOption
        }
        println "new Option: "
        println newOption
        println "new Answer: "
        println keyNew
    }

//    static void main(String[] args) {
//        def option = [
//                "A": "java EE",
//                "B": "java SE",
//                "C": "java ME",
//                "D": "java HE",
//        ]
//        RandomOption.random(option, "D")
//    }
}
